package weibo.acitivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.orm.SugarRecord;
import com.whu.administrator.ws.R;
import com.whu.administrator.ws.UIHandler;


import java.util.ArrayList;
import java.util.List;

import inter.Delegate;
import inter.ResponseStatus;
import model.Blog;
import weibo.adapter.BokeAdapter;
import weibo.network.GetWeibo;

/**
 * Created by Administrator on 2016/3/31.
 */
public class BokeActivity extends UIHandler.BaseActivity {

    PullToRefreshListView mPulltoRefreshListView;

    ListView lv_blog;

    Button btn_send;

    // 微博
    List<Blog> blogs = new ArrayList<Blog>();

    BokeAdapter bokeAdapter = new BokeAdapter();

    {
        bokeAdapter.setBlogs(blogs);
    }


    // 获取最初的值
    Delegate getInitBoke = new Delegate() {
        @Override
        public void onResponse(String response) {
            if (!response.startsWith("{")) return;
            Gson gson = new Gson();
            GetWeibo.ResponseGetBlog responseGetBlog = gson.fromJson(response, GetWeibo.ResponseGetBlog.class);
            if (responseGetBlog.getStatus().equals(ResponseStatus.SUCCESS)) {
                List<weibo.dto.Blog> blogDatas = responseGetBlog.getBlogList();
                List<Blog> tmps = new ArrayList<Blog>();
                for (weibo.dto.Blog blogData : blogDatas) {
                    Blog blog = new Blog();
                    BlogData2Blog(blog,blogData);
                    tmps.add(blog);
                }
                blogs.clear();
                blogs.addAll(tmps);
                bokeAdapter.notifyDataSetChanged();
            }
        }

        @Override
        public void onErrorResponse(VolleyError error) {

        }
    };

    public void BlogData2Blog(Blog blog, weibo.dto.Blog blogData)
    {
        blog.setImage_Set(blogData.getImage_Set());
        blog.setBlog_ID(blogData.getBlog_ID());
        blog.setBlog_Text(blogData.getBlog_Text());
        blog.setBlog_Time(blogData.getBlog_Time());
        blog.setBlog_Type(blogData.getBlog_Type());
        blog.setImage_Num(blogData.getImage_Num());
        blog.setUser_Name(blogData.getUser_Name());
    }

    // 获取最新博客
    Delegate getNewBoke = new Delegate() {
        @Override
        public void onResponse(String response) {
            mPulltoRefreshListView.onRefreshComplete();
            if (!response.startsWith("{")) return;
            Gson gson = new Gson();
            GetWeibo.ResponseGetBlog responseGetBlog = gson.fromJson(response, GetWeibo.ResponseGetBlog.class);
            if (responseGetBlog.getStatus().equals(ResponseStatus.SUCCESS)) {
                List<weibo.dto.Blog> blogDatas = responseGetBlog.getBlogList();
                List<Blog> tmps = new ArrayList<Blog>();
                for (weibo.dto.Blog blogData : blogDatas) {
                    Blog blog = new Blog();
                    BlogData2Blog(blog,blogData);
                    tmps.add(blog);
                }
                blogs.addAll(0,tmps);
                bokeAdapter.notifyDataSetChanged();
            }
        }

        @Override
        public void onErrorResponse(VolleyError error) {
            mPulltoRefreshListView.onRefreshComplete();
        }
    };

    // 获取以前的博客
    Delegate getOldBoke = new Delegate() {
        @Override
        public void onResponse(String response) {
            mPulltoRefreshListView.onRefreshComplete();
            if (!response.startsWith("{")) return;
            Gson gson = new Gson();
            GetWeibo.ResponseGetBlog responseGetBlog = gson.fromJson(response, GetWeibo.ResponseGetBlog.class);
            if (responseGetBlog.getStatus().equals(ResponseStatus.SUCCESS)) {
                List<weibo.dto.Blog> blogDatas = responseGetBlog.getBlogList();
                List<Blog> tmps = new ArrayList<Blog>();
                for (weibo.dto.Blog blogData : blogDatas) {
                    Blog blog = new Blog();
                    BlogData2Blog(blog,blogData);
                    tmps.add(blog);
                }

                blogs.addAll(tmps);
                bokeAdapter.notifyDataSetChanged();
            }
        }

        @Override
        public void onErrorResponse(VolleyError error) {
            mPulltoRefreshListView.onRefreshComplete();
        }
    };

    GetWeibo getWeiboInit = new GetWeibo();

    {
        getWeiboInit.setDelegate(getInitBoke);
    }

    GetWeibo getWeiboOld = new GetWeibo();

    {
        getWeiboOld.setDelegate(getOldBoke);
    }

    GetWeibo getWeiboNew = new GetWeibo();

    {
        getWeiboNew.setDelegate(getNewBoke);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_boke);
        initview();
        initBlog();
    }

    private void initBlog() {

        List<Blog> blogsTmp = SugarRecord.listAll(Blog.class);
        blogs.addAll(blogsTmp);
        bokeAdapter.notifyDataSetChanged();

        GetWeibo.RequestBlog requestBlog = new GetWeibo.RequestBlog();
        requestBlog.setIsNew("2");
        getWeiboInit.setRequestBlog(requestBlog);
        getWeiboInit.get();
    }

    private void getNew()
    {
        GetWeibo.RequestBlog requestBlog = new GetWeibo.RequestBlog();
        requestBlog.setIsNew("1");
        if (blogs.isEmpty()) {
            mPulltoRefreshListView.onRefreshComplete();
            return;
        }
        requestBlog.setBlog_ID(blogs.get(0).getBlog_ID());
        getWeiboNew.setRequestBlog(requestBlog);
        getWeiboNew.get();
    }

    private void getMore()
    {
        GetWeibo.RequestBlog requestBlog = new GetWeibo.RequestBlog();
        requestBlog.setIsNew("0");
        if (blogs.isEmpty())
        {
            mPulltoRefreshListView.onRefreshComplete();
            return;
        }
        requestBlog.setBlog_ID(blogs.get(blogs.size() - 1).getBlog_ID());
        getWeiboOld.setRequestBlog(requestBlog);
        getWeiboOld.get();
    }


    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v == btn_send) {
                Intent intent = new Intent(BokeActivity.this, SendWeiboActivity.class);
                BokeActivity.this.startActivity(intent);
            }
        }
    };

    // 初始化界面
    private void initview() {
        mPulltoRefreshListView = (PullToRefreshListView) findViewById(R.id.id_lv_boke);
        lv_blog = mPulltoRefreshListView.getRefreshableView();
        lv_blog.setAdapter(bokeAdapter);
        lv_blog.setDividerHeight((int)getResources().getDimension(R.dimen.y20));
        mPulltoRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
        mPulltoRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                Log.i("pull down", "下拉");
                getNew();

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                getMore();
                Log.i("pull up", "上拉");

            }
        });
        btn_send = (Button) findViewById(R.id.id_btn_send);
        btn_send.setOnClickListener(onClickListener);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        SugarRecord.deleteAll(Blog.class);
        SugarRecord.saveInTx(blogs);
    }
}
